package inf3791.calendarsystem.others.components;

import inf3791.calendarsystem.shared.Constants;
import java.awt.Font;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Calendar;
import javax.swing.JButton;
import javax.swing.SpinnerNumberModel;

/**
 * Renders a simple way to select a month and a year.
 *
 * @author Jonas Weber
 */
public class MonthSelector extends javax.swing.JPanel implements PropertyChangeListener {

    @Override
    public void propertyChange(PropertyChangeEvent pce) {
        if (master != null) {
            this.month = master.month - 1;
            labelMonth.setText(Constants.MONTHNAMES[this.month]);

            spinYearSelector.setValue(master.spinYearSelector.getValue());
            
        }
    }

    public enum Style {

        BOTH,
        ONLY_LEFT,
        ONLY_RIGHT,
        NONE
    }
    private Style style = Style.BOTH;

    public Style getStyle() {
        return style;
    }

    private void applyStyle(Style secondPossibility, JButton but) {
        but.setVisible(style == Style.BOTH || style == secondPossibility);
    }

    public void setStyle(Style style) {
        Style ov = this.style;

        this.style = style;

        applyStyle(Style.ONLY_LEFT, butLeft);
        applyStyle(Style.ONLY_RIGHT, butRight);


        firePropertyChange("style", ov, style);
    }
    private MonthSelector master;

    public MonthSelector getMaster() {
        return master;
    }

    public void setMaster(MonthSelector master) {
        if (this.master != null)
        {
            this.master.removePropertyChangeListener(this);
        }
        
        this.master = master;

        if (master != null)
        {
            master.addPropertyChangeListener(this);
        }
    }
    private int month;
    private int year;

    /*@Override
    public Font getFont() {
        if (labelMonth != null) {
            return labelMonth.getFont();
        } else {
            return null;
        }
    }*/

    @Override
    public void setFont(Font nf) {
        
        super.setFont(nf);
        if (labelMonth != null) {
            labelMonth.setFont(nf);
            spinYearSelector.setFont(nf);
            spinYearSelector.getEditor().setFont(nf);
            butLeft.setFont(nf);
            butRight.setFont(nf);
        }
    }

    public final int getMonth() {
        return month + 1;
    }

    public final void setMonth(int month) {
        int om = month;
        this.month = month - 1;

        labelMonth.setText(Constants.MONTHNAMES[this.month]);

        /*if (master != null)
        {
            master.setActionMap(null)
        }*/
        
        firePropertyChange("month",
                om, this.month);
    }

    public final int getYear() {
        return year;
    }

    public final void setYear(int year) {
        int oy = this.year;

        spinYearSelector.setValue(year);
        this.year = year;

        firePropertyChange("year", oy, year);       
    }

    /**
     * Creates new form MonthSelector
     */
    public MonthSelector() {
        initComponents();

        setFont(getFont());
        
        Calendar c = Calendar.getInstance();

        setMonth(c.get(Calendar.MONTH) + 1);
        setYear(c.get(Calendar.YEAR));
    }

    void increase() {
        int m = getMonth() + 1;
        if (m == 13) {
            m = 1;

            Object o = spinYearSelector.getNextValue();

            if (o != null) {
                Object oy = spinYearSelector.getValue();
                spinYearSelector.setValue(o);
                firePropertyChange("year", oy, getYear());

                setMonth(m);
            }
        } else {
            setMonth(m);
        }
    }

    void decrease() {
        int m = getMonth() - 1;
        if (m == 0) {
            m = 12;
            Object o = spinYearSelector.getPreviousValue();
            if (o != null) {
                Object oy = spinYearSelector.getValue();
                spinYearSelector.setValue(o);
                firePropertyChange("year", oy, year);

                setMonth(m);
            }
        } else {
            setMonth(m);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        butLeft = new javax.swing.JButton();
        labelMonth = new javax.swing.JLabel();
        spinYearSelector = new javax.swing.JSpinner();
        butRight = new javax.swing.JButton();

        butLeft.setFont(new java.awt.Font("Dialog", 1, 24)); // NOI18N
        butLeft.setText("◄");
        butLeft.setMinimumSize(new java.awt.Dimension(20, 39));
        butLeft.setPreferredSize(new java.awt.Dimension(20, 39));
        butLeft.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                butLeftActionPerformed(evt);
            }
        });

        labelMonth.setFont(new java.awt.Font("Dialog", 1, 24)); // NOI18N
        labelMonth.setText("October");

        spinYearSelector.setModel(new javax.swing.SpinnerNumberModel());
        spinYearSelector.setEditor(new javax.swing.JSpinner.NumberEditor(spinYearSelector, "0000"));
        spinYearSelector.setName(""); // NOI18N
        spinYearSelector.setRequestFocusEnabled(false);
        spinYearSelector.setValue(2012);
        spinYearSelector.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                spinYearSelectorStateChanged(evt);
            }
        });

        butRight.setFont(new java.awt.Font("Dialog", 1, 24)); // NOI18N
        butRight.setText("►");
        butRight.setMaximumSize(null);
        butRight.setMinimumSize(new java.awt.Dimension(20, 39));
        butRight.setPreferredSize(new java.awt.Dimension(20, 39));
        butRight.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                butRightActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(butLeft, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelMonth)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 102, Short.MAX_VALUE)
                .addComponent(spinYearSelector, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(butRight, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(butLeft, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(labelMonth)
                .addComponent(spinYearSelector, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(butRight, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void butLeftActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_butLeftActionPerformed
        decrease();
    }//GEN-LAST:event_butLeftActionPerformed

    private void butRightActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_butRightActionPerformed
        increase();
    }//GEN-LAST:event_butRightActionPerformed

    private void spinYearSelectorStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_spinYearSelectorStateChanged
        setYear(((SpinnerNumberModel) spinYearSelector.getModel()).getNumber().intValue());
    }//GEN-LAST:event_spinYearSelectorStateChanged
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton butLeft;
    private javax.swing.JButton butRight;
    private javax.swing.JLabel labelMonth;
    private javax.swing.JSpinner spinYearSelector;
    // End of variables declaration//GEN-END:variables
}
